/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.hsos.samcomm.userManagement.boundary.jsf;

import de.hsos.samcomm.boundary.Community;
import de.hsos.samcomm.userManagement.entity.Member;
import de.hsos.samcomm.common.MessageHandler;
import de.hsos.samcomm.userManagement.controller.MemberDAO;
import java.io.Serializable;
import java.util.MissingResourceException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;

/**
 *
 * @author Dennis
 */
@ManagedBean(name="loginBean")
@SessionScoped
public class LoginBean implements Serializable {
    @Inject
    Community community;
    private String nickname;
    private String password;
    private Member actMember = null;

    /**
     * Default Konstruktor.
     */
    public LoginBean() {}

    /**
     * Prueft das Login des Benutzers.
     *
     * @return Adresse der naechsten Seite
     */
    public String login() {
        try {
            boolean result = (community.login(nickname, password));

            if (!result) {
                MessageHandler.addContextFacesMessage(FacesMessage.SEVERITY_WARN, "login.message.failed", "login.message.failed.info");
            }
            else {
                for(Member m : community.getMember()) {
                    if (m.getNickname().equals(nickname)) {
                        actMember = m;
                    }
                }
            }

            clear();
            return "LOGIN";
        }
        catch (MissingResourceException ex) {
            MessageHandler.addExceptionFacesMessage(ex);
        }

        return "";
    }

    /**
     * Get-Methode fuer den Benutzernamen
     *
     * @return nickname
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * Get-Methode fuer das Passwort.
     *
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Get-Methode fuer den aktuellen Benutzer.
     *
     * @return aktueller Benutzer
     */
    public Member getActMember() {
        return actMember;
    }

    /**
     * Set-Methode fuer den Benutzernamen.
     *
     * @param nickname
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    /**
     * Set-Methode fuer das Passwort.
     *
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Set-Methode fuer den aktuellen Benutzer.
     *
     * @param actMember
     */
    public void setActMember(Member actMember) {
        this.actMember = actMember;
    }

    /**
     * Setzt alle Strings zurueck.
     */
    private void clear() {
        nickname = "";
        password = "";
    }
}
